//
//  CompanyFindPassViewController.m
//  CarTown
//
//  Created by 熊浩 on 15/7/31.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "CompanyFindPassViewController.h"
#import "Utils.h"
#import "LCActionSheet.h"
#import "IWUploadParam.h"
#import "ResMode.h"
#import "ShengfenShowViewController.h"
#import "WeiTuoViewController.h"
#import "FaRenViewController.h"
#define SCALEFlOALT 0.8
@interface CompanyFindPassViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,LCActionSheetDelegate>
@property (weak, nonatomic) IBOutlet UITextField *com_username;
@property (weak, nonatomic) IBOutlet UITextField *com_name;
@property (weak, nonatomic) IBOutlet UITextField *com_cardnumber;
@property (weak, nonatomic) IBOutlet UILabel *support_lable;
@property (weak, nonatomic) IBOutlet UIButton *yingye;
@property (weak, nonatomic) IBOutlet UIButton *back;
@property (weak, nonatomic) IBOutlet UIButton *follwo;
@property (weak, nonatomic) IBOutlet UIButton *hold;
@property (weak, nonatomic) IBOutlet UIButton *weituo;
@property (nonatomic,strong) UIImagePickerController *picker;
@property (nonatomic,strong) UIImage *yingyeimg;
@property (nonatomic,strong) UIImage *backimg;
@property (nonatomic,strong) UIImage *follwoimg;
@property (nonatomic,strong) UIImage *holdimg;
@property (nonatomic,strong) UIImage *weituoimg;
@property (nonatomic,strong) UUImageAvatarBrowser *brower;
@property (nonatomic,strong) NSArray *btnarray;
@end

@implementation CompanyFindPassViewController
+(instancetype)companyfind{
    CompanyFindPassViewController *company=[XHMianStory instantiateViewControllerWithIdentifier:@"companyfind"];
    
    return company;
}
-(NSArray *)btnarray{
    if (_btnarray==nil) {
        _btnarray=@[self.yingye,self.back,self.follwo,self.hold,self.weituo];
    }
    return _btnarray;
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

-(void)showActionSheet:(NSInteger)tag{
    
    LCActionSheet *sheet=[[LCActionSheet alloc]initWithTitle:nil buttonTitles:@[@"拍照",@"从相册中选取"] redButtonIndex:-1 delegate:self];
    sheet.tag=tag;
    sheet.isTag=NO;
    [sheet show];
}
- (IBAction)weituoclick:(UIButton *)sender {
    if (self.weituoimg==nil) {
        [self showActionSheet:sender.tag];
    }else{
        [self showHasActionSheet:sender.tag];
    }
}
- (IBAction)holdclick:(UIButton *)sender {
    if (self.holdimg==nil) {
        [self showActionSheet:sender.tag];
    }else{
        [self showHasActionSheet:sender.tag];
    }
}
- (IBAction)followclick:(UIButton *)sender {
    if (self.follwoimg==nil) {
        [self showActionSheet:sender.tag];
    }else{
        [self showHasActionSheet:sender.tag];
    }
}
- (IBAction)backclick:(UIButton *)sender {
    if (self.backimg==nil) {
        [self showActionSheet:sender.tag];
    }else{
        [self showHasActionSheet:sender.tag];
    }
}
- (IBAction)yingyeclick:(UIButton *)sender {
    if (self.yingyeimg==nil) {
        [self showActionSheet:sender.tag];
    }else{
        [self showHasActionSheet:sender.tag];
    }
}
-(void)showHasActionSheet:(NSInteger)tag{
    
    LCActionSheet *sheet=[[LCActionSheet alloc]initWithTitle:nil buttonTitles:@[@"查看大图",@"重新上传"] redButtonIndex:-1 delegate:self];
    sheet.tag=tag;
    sheet.isTag=YES;
    [sheet show];
}
-(void)showCarmera:(NSInteger)tag{
    UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeCamera;
    self.picker.view.tag=tag;
    self.picker.sourceType=sourcheType;
    [self presentViewController:self.picker animated:YES completion:nil];
}
-(void)showImagePicker:(NSInteger)tag{
    UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypePhotoLibrary;
    self.picker.sourceType=sourcheType;
    self.picker.view.tag=tag;
    [self presentViewController:self.picker animated:YES completion:nil];
}

-(UIImagePickerController *)picker{
    if (_picker==nil) {
        _picker=[[UIImagePickerController alloc]init];
        _picker.view.backgroundColor = [UIColor orangeColor];
        
        _picker.delegate =self;
        _picker.allowsEditing = YES;
    }
    return _picker;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"公司找回密码";
    // Do any additional setup after loading the view.
    NSMutableAttributedString *str=[[NSMutableAttributedString alloc]initWithString:@"客服电话 400-822-7177"];
    self.navigationController.navigationBarHidden=NO;
    int strlength=str.length;
    [str addAttribute:NSForegroundColorAttributeName value:tintblue range:NSMakeRange(5,strlength-5)];
    self.support_lable.attributedText=str;
   
}

- (IBAction)commit:(id)sender {
    if ([self checkForm]) {
        [self upload];
    }
    
    
}

-(void)upload{
    
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    hud.labelText=@"正在上传...";
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.margin = 10.f;
    hud.removeFromSuperViewOnHide = YES;
    NSMutableArray *photoarray=[NSMutableArray array];
    IWUploadParam *yingyeparam=[[IWUploadParam alloc]init];
    yingyeparam.data=[self.yingyeimg compressData:self.yingyeimg toMaxFileSize:DefailutPicSize];
    yingyeparam.mineType=@"jpeg";
    yingyeparam.paramName=@"zhizhao_pic";
    yingyeparam.fileName=@"zhizhao_pic.jpeg";
    [photoarray addObject:yingyeparam];
    IWUploadParam *namez=[[IWUploadParam alloc]init];
    namez.data= [self.follwoimg compressData:self.follwoimg toMaxFileSize:DefailutPicSize];
    namez.mineType=@"jpeg";
    namez.paramName=@"user_z";
    namez.fileName=@"user_z.jpeg";
    [photoarray addObject:namez];
    IWUploadParam *namef=[[IWUploadParam alloc]init];
    namef.data=[self.backimg compressData:self.backimg toMaxFileSize:DefailutPicSize];
    namef.mineType=@"jpeg";
    namef.paramName=@"user_f";
    namef.fileName=@"user_f.jpeg";
    [photoarray addObject:namef];
    IWUploadParam *handwith=[[IWUploadParam alloc]init];
   
    handwith.data=[self.holdimg compressData:self.holdimg toMaxFileSize:DefailutPicSize];
    handwith.mineType=@"jpeg";
    handwith.paramName=@"user_h";
    handwith.fileName=@"user_h.jpeg";
    [photoarray addObject:handwith];
    IWUploadParam *weituo=[[IWUploadParam alloc]init];
    weituo.data=[self.weituoimg compressData:self.weituoimg toMaxFileSize:DefailutPicSize];
    weituo.mineType=@"jpeg";
    weituo.paramName=@"f_book";
    weituo.fileName=@"f_book.jpeg";
    [photoarray addObject:weituo];
        NSMutableDictionary *params=[NSMutableDictionary dictionary];
    params[@"action"]=@"company";
    
    params[@"c_username"]=self.com_username.text;
  
    params[@"c_name"]=self.com_name.text;
    params[@"c_zhizhao"]=self.com_cardnumber.text;
    [PostUitls upload:makepassurl parameters:params uploadarray:photoarray success:^(id responseobj) {
        NSDictionary *dict=responseobj;
        ResMode *mode=[ResMode modeWithDict:dict];
        NSLog(@"%@",mode.msg);
        if ([mode.state integerValue]==400) {
//            [self pushToSuS:mode];
        }
        [hud hide:YES afterDelay:0.5];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [hud hide:YES afterDelay:0.5];
    }];
    
}

- (IBAction)fanliclick:(UIButton *)sender {
    
    switch (sender.tag) {
        case 0:
        {
            WeiTuoViewController *vc=[[WeiTuoViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1:
        {
            ShengfenShowViewController*vc=[[ShengfenShowViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            FaRenViewController *vc=[[FaRenViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        default:
            break;
    }
    
}


-(BOOL)checkForm{
    if (self.com_username.text.length==0) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"公司账号不能为空";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return false;
    }
    if (self.com_name.text.length==0) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"公司名称不能为空";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return false;
    }
    if (self.com_cardnumber.text.length==0) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"营业执照不能为空";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return false;
    }
        if (self.yingyeimg==nil) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"营业执照照片不能为空";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return false;
    }
    if (self.backimg==nil) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"身份证背面照片不能为空";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return false;
    }
    if (self.follwoimg==nil) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"身份证正面照片不能为空";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return false;
    }
    if (self.holdimg==nil) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"手持身份证照片不能为空";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return false;
    }
    if (self.weituoimg==nil) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"委托书照片不能为空";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return false;
    }

    return true;
}


-(void)switchImagePickerOrActionsheet:(BOOL)ishasimg WithTag:(NSInteger)tag index:(NSInteger)buttonindex{
    if (buttonindex==0) {
        [self showCarmera:tag];
    }else{
        [self showImagePicker:tag];
    }
    
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [self dismissViewControllerAnimated:YES completion:^{
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        
        NSLog(@"%f,%f",image.size.width,image.size.height);
        switch (picker.view.tag) {
            case 0:
                self.yingyeimg=image;
                [self.yingye setImage:image forState:UIControlStateNormal];
                break;
            case 1:
                self.backimg=image;
                [self.back setImage:image forState:UIControlStateNormal];
                break;
            case 2:
                self.follwoimg=image;
                [self.follwo setImage:image forState:UIControlStateNormal];
                break;
            case 3:
                self.holdimg=image;
                [self.hold setImage:image forState:UIControlStateNormal];
                break;
            case 4:
                self.weituoimg=image;
                [self.weituo setImage:image forState:UIControlStateNormal];
                break;
            default:
                break;
        }
        
    }];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)actionSheet:(LCActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%ld---index--%ld",actionSheet.tag,buttonIndex);
    
    if (buttonIndex==2) return;
    if (actionSheet.isTag) {
        if (buttonIndex==0) {
            UIButton *btn=self.btnarray[actionSheet.tag];
            UUImageAvatarBrowser *brower=[[UUImageAvatarBrowser alloc]init];
            [brower showImage:btn.imageView];
            self.brower=brower;
        }else{
            [self showActionSheet:actionSheet.tag];
        }
    }else{
        switch (actionSheet.tag) {
            case 0:
            {
                [self switchImagePickerOrActionsheet:self.yingyeimg WithTag:actionSheet.tag index:buttonIndex];
            }
                break;
            case 1:
                [self switchImagePickerOrActionsheet:self.backimg WithTag:actionSheet.tag index:buttonIndex];
                break;
                
            case 2:
                [self switchImagePickerOrActionsheet:self.follwoimg WithTag:actionSheet.tag index:buttonIndex];
                break;
                
            case 3:
                [self switchImagePickerOrActionsheet:self.holdimg WithTag:actionSheet.tag index:buttonIndex];
                break;
            case 4:
                [self switchImagePickerOrActionsheet:self.weituoimg WithTag:actionSheet.tag index:buttonIndex];
                break;
                
            default:
                break;
        }
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
